from re_common.baselibrary.utils.core.mdeprecated import try_except2, try_except2_async

from apps.crawler_platform.core_callback import *
from apps.crawler_platform.core_platform.core_controller import CoreConterollerMixin
from apps.crawler_platform.core_platform.core_g import SQLTable
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import DownModel, HtmlModel, PolicyArticleModel
from apps.crawler_platform.policy_platform.base_qk_step import BaseQKItems, BaseQKStep

# 不执行逻辑，目的是为了不让 from apps.crawler_platform.core_callback import *
# 被编辑器认为没有使用，快捷清除时就不会被清除
STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()


class PolicyArticleItems(BaseQKItems):

    def __init__(self):
        super(PolicyArticleItems, self).__init__()
        self.rawid = ""

    def get_pkm(self):
        """
        获取主键
        :return:
        """
        return {"task_name": self.task_name,
                "task_tag": self.task_tag,
                "rawid": self.rawid,
                }

    def set_downmodel(self):
        self.down_model = DownModel[HtmlModel](down_dict={})


class PolicyArticleDownStep(BaseQKStep):

    def __init__(self, request, inputs):
        super().__init__()

        self.err_tag = "BaikeArticle"

        self.pm = PolicyArticleItems()
        self.pm.set_downmodel()
        self.input = inputs
        self.pm.task_name = inputs.data.task_name
        self.pm.task_tag = inputs.data.task_tag
        self.pm.rawid = inputs.data.rawid

        self.pm.redis = request.app.state.redis

        self.request = request
        self.step_info = ""
        # 解析字段字典
        self.para_dicts = {}

        self.sql_model: PolicyArticleModel
        self.task_table = SQLTable.policy_article

    @try_except2_async(is_print=True)
    async def deal_html(self):
        return await super().deal_html(PolicyArticleModel)

    @try_except2_async(is_print=True)
    async def save_sql(self, type="", code=""):
        """
        与关系型sql的处理 包括存储下一级的sql以及本级sql的状态和字段更新
        """
        sql_table = SQLTable.policy_article
        sql_next_table = ""
        bools, msg = await super().save_sql(sql_table, sql_next_table, type, code)
        if bools and self.pm.get_sql_state().get("state", -1) == 0 and self.pm.turn_page != 4:
            return False, {"msg": "该错误记录到mysql的err_msg,请查看,policy_article 执行sql成功，但状态为 0，无错误码"}
        return bools, msg

    @try_except2_async(is_print=True)
    async def get_sql(self):
        row = await CoreSqlMixin.get_policy_article(self.pm.task_name, self.pm.task_tag,
                                                    self.pm.rawid)
        if row:
            self.pm.sql_model = PolicyArticleModel.parse_obj(row)
            return True, row
        else:
            return False, "sql查询不存在"

    @try_except2_async(is_print=True)
    async def save_nosql_html(self):
        bools, dicts = await super().save_nosql_html()
        return bools, dicts

